package ip.controller;
/***********************************************************************
 * Module:  CreateMovieController.java
 * Author:  mojsilo
 * Purpose: Defines the Class CreateMovieController
 ***********************************************************************/

import ip.command.CreateMovieCommand;
import ip.dto.CategoryDTO;
import ip.dto.CountryDTO;
import ip.dto.LanguageDTO;
import ip.dto.MovieDTO;
import ip.dto.UserDTO;
import ip.exceptions.ApplicationException;

import java.io.IOException;
import java.sql.Time;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

/**
 * @web.servlet
 * 	name="CreateMovieController"
 * @web.servlet-mapping
 * 	url-pattern="/control/CreateMovie"
 */
public class CreateMovieController extends HttpServlet
{

	private static final long serialVersionUID = -6558054252658563616L;
	private Logger logger = Logger.getLogger(this.getClass());

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
	{
		try{
			logger.info("doGet(...)");
			HttpSession session = request.getSession(true);      
			UserDTO adminDto = (UserDTO) session.getAttribute("administrator");			

			if(adminDto == null){
				response.sendRedirect(response.encodeRedirectURL("../jsp/login_administrator.jsp"));
				return;
			}

			CountryDTO countryDTO = new CountryDTO();
			countryDTO.setCountryId(Long.parseLong(request.getParameter("countryId)")));
			CategoryDTO categoryDTO = new CategoryDTO();
			categoryDTO.setCategoryId(Long.parseLong(request.getParameter("categoryId)")));
			LanguageDTO languageDTO = new LanguageDTO();
			languageDTO.setLanguageId(Long.parseLong(request.getParameter("languageId)")));

			MovieDTO movieDTO = new MovieDTO();
			
			movieDTO.setLength(new Time(Integer.parseInt(request.getParameter("hour")),Integer.parseInt(request.getParameter("minute")),Integer.parseInt(request.getParameter("second"))));

			movieDTO.setDescription(request.getParameter("description"));
			movieDTO.setDirector(request.getParameter("director"));
			movieDTO.setOriginalTitle(request.getParameter("originalTitle"));
			movieDTO.setPrice(Float.parseFloat(request.getParameter("price")));
			movieDTO.setProducer(request.getParameter("producer"));
			movieDTO.setQuantity(Integer.parseInt(request.getParameter("quantity")));
			movieDTO.setTranslatedTitle(request.getParameter("translatedTitle"));
			movieDTO.setYear(Integer.parseInt(request.getParameter("year")));
			
			logger.debug("doGet(...) - movieDTO: "+movieDTO);
			CreateMovieCommand createMovieCommand = new CreateMovieCommand();
			createMovieCommand.setMovieDTO(movieDTO);
			createMovieCommand.init();
			createMovieCommand.execute();
			createMovieCommand.dispose();
			getServletConfig().getServletContext().getRequestDispatcher("/control/Movies").forward(request, response);
		}
		catch (ApplicationException e) {
			logger.error(e);
			request.setAttribute("error", e.getMessage());
			getServletConfig().getServletContext().getRequestDispatcher("error.jsp").forward(request, response);
		}
		catch (Exception e) {
			logger.error(e);
			request.setAttribute("error", "Unexpected error!!");//kako ovo bolje odraditi???
			getServletConfig().getServletContext().getRequestDispatcher("error.jsp").forward(request, response);
		}


	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
	{
		logger.info("doPost(...)");
		doGet(request, response);
	}

}